package com.xgggh.redpack.ui.redpack.viewbinder;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;

import com.jakewharton.rxbinding3.view.RxView;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.viewbinder.BinderViewHolder;
import com.xgggh.redpack.databinding.ContentCategoryItemBinding;
import com.xgggh.redpack.domain.app.IntentFlag;
import com.xgggh.redpack.domain.category.CategoryItem;
import com.xgggh.redpack.ui.redpack.RedPackActivity;

import java.util.concurrent.TimeUnit;

import io.reactivex.functions.Consumer;
import me.drakeet.multitype.ItemViewBinder;

public class CategoryViewBinder extends ItemViewBinder<CategoryItem, BinderViewHolder> {

    @NonNull
    @Override
    protected BinderViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
      ContentCategoryItemBinding binding= DataBindingUtil.inflate(inflater,R.layout.content_category_item,parent,false);
        return new BinderViewHolder(binding.getRoot());
    }

    @Override
    protected void onBindViewHolder(@NonNull BinderViewHolder holder, @NonNull CategoryItem item) {
        ContentCategoryItemBinding binding= DataBindingUtil.getBinding(holder.itemView);
        binding.setItem(item);
        RxView.clicks(binding.container)
                .throttleFirst( 2 , TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        CategoryItem categoryItem=(CategoryItem)   binding.container.getTag();
                        onItemClick( binding.container.getContext(),categoryItem);
                    }
                });
    }

    public void onItemClick(Context context,CategoryItem categoryItem){
        Intent intent=new Intent(context,RedPackActivity.class);
        intent.putExtra(IntentFlag.data,categoryItem);
        context.startActivity(intent);
    }

}
